package com.zrar.ak.gkpt.service.impl.common;

import com.zrar.ak.gkpt.dao.IFormProcessNodeTDao;
import com.zrar.ak.gkpt.model.bo.FormProcessNodeTBO;
import com.zrar.ak.gkpt.model.vo.NodeCirculeInfoVO;
import com.zrar.ak.gkpt.service.common.IProcessService;
import com.zrar.ak.gkpt.util.CommonTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.util.StringUtil;

@Service
public class ProcessService implements IProcessService {

    @Autowired
    private IFormProcessNodeTDao formProcessNodeTDao;

    @Override
    public FormProcessNodeTBO getStartOrEndNodeByProcessDefinitionIdAndNodeType(String processDefinitionId, String nodeType) throws Exception{
        FormProcessNodeTBO formProcessNodeTBO = new FormProcessNodeTBO();
        formProcessNodeTBO.setProcessDefinitionId(processDefinitionId);
        formProcessNodeTBO.setNodeType(nodeType);
        formProcessNodeTBO = formProcessNodeTDao.selectOne(formProcessNodeTBO);
        formProcessNodeTBO = nodeCirculeInfoTransformNodeCirculeInfoVO(formProcessNodeTBO);
        return formProcessNodeTBO;
    }

    @Override
    public FormProcessNodeTBO getNodeByNodeId(String nodeId) throws Exception {
        FormProcessNodeTBO formProcessNodeTBO = formProcessNodeTDao.selectByPrimaryKey(nodeId);
        formProcessNodeTBO = nodeCirculeInfoTransformNodeCirculeInfoVO(formProcessNodeTBO);
        return formProcessNodeTBO;
    }


    /**
     * 将formProcessNodeTBO实例中的nodeCirculeInfo属性值转换成NodeCirculeInfoVO的实例并赋值nodeCirculeInfoVO属性
     * @param formProcessNodeTBO
     * @return
     */
    private static FormProcessNodeTBO nodeCirculeInfoTransformNodeCirculeInfoVO(FormProcessNodeTBO formProcessNodeTBO) throws Exception{
        String nodeCirculeInfo = formProcessNodeTBO.getNodeCirculeInfo();
        if(StringUtil.isNotEmpty(nodeCirculeInfo)){
            NodeCirculeInfoVO nodeCirculeInfoVO = CommonTools.jsonStrToObject(nodeCirculeInfo, NodeCirculeInfoVO.class);
            formProcessNodeTBO.setNodeCirculeInfoVO(nodeCirculeInfoVO);
        }
        return formProcessNodeTBO;
    }


}
